package cn.anlab.anappframe.activity;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher;
import cn.anlab.anappframe.R;

public class PhotoActivity extends Activity implements OnTouchListener {

	private ImageSwitcher switcher;
	private Gallery gallery;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo);

		switcher = (ImageSwitcher) findViewById(R.id.switcher);
		ivPhoto = (ImageView)findViewById(R.id.ivPhoto);
		gallery = (Gallery) findViewById(R.id.gallery);

		// 设置图片生成工厂
		switcher.setFactory(new ViewSwitcher.ViewFactory() {
			@Override
			public View makeView() {
				ImageView iv = new ImageView(PhotoActivity.this);
				iv.setScaleType(ScaleType.MATRIX);
				iv.setOnTouchListener(PhotoActivity.this);
				return iv;
			}
		});

		// 设置图片切换的动画
		switcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));

		// 显示中间那张图片
		switcher.setImageDrawable(getResources().getDrawable(
				ids[ids.length / 2]));

		switcher.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});

		// Gallery
		// Gallery gallery = new Gallery(this) {
		// @Override
		// public boolean onFling(MotionEvent e1, MotionEvent e2,
		// float velocityX, float velocityY) {
		// return false;
		// }
		// };
		gallery.setSpacing(1); // 需要设置此属性，不然两图片边缘会有部分重叠在一起
		ColorDrawable drawable = new ColorDrawable(Color.WHITE);
		drawable.setAlpha(200);
		gallery.setBackgroundDrawable(drawable);
		gallery.setPadding(5, 5, 5, 5);
		gallery.setAdapter(imgAdapter);

		// 点击事件，切换大图
		gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ivPhoto.setImageResource(ids[position]);
				selectPos = position;
				switcher.setImageDrawable(getResources().getDrawable(
						ids[position]));
			}

		});
		// 选中中间那张
		gallery.setSelection(ids.length / 2, true);
		
		detector = new GestureDetector(this, gestureListener);
		
		System.out.println("count = " + switcher.getChildCount());

	}
	
	private int selectPos = 0;

	int[] ids = { R.drawable.girl2, R.drawable.girl1, R.drawable.girl3,
			R.drawable.girl4, R.drawable.girl5 };

	BaseAdapter imgAdapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = new ImageView(PhotoActivity.this);
				convertView.setLayoutParams(new Gallery.LayoutParams(120, 80));
				((ImageView) convertView).setScaleType(ScaleType.CENTER_CROP);
			}
			Drawable drawable = getResources().getDrawable(ids[position]);
			((ImageView) convertView).setImageDrawable(drawable);
			return convertView;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public int getCount() {
			return ids.length;
		}
	};
	
	// Matrix instances to move and zoom image
	Matrix matrix = new Matrix();
	Matrix eventMatrix = new Matrix();
	// possible touch states
	final static int NONE = 0;
	final static int DRAG = 1;
	final static int ZOOM = 2;
	int touchState = NONE;	
	

	final static float MIN_DIST = 50;
	static float eventDistance = 0;
	static float centerX = 0, centerY = 0;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return detector.onTouchEvent(event);
//		ImageView view = (ImageView) v;
//		switch (event.getAction() & MotionEvent.ACTION_MASK) {
//		case MotionEvent.ACTION_DOWN:
//			Log.i("MultitouchTestActivity", "ACTION_DOWN, (" + event.getX(0) + ", " + event.getY(0) + ")");
//			// primary touch event starts: remember touch down location
//			touchState = DRAG;
//			centerX = event.getX(0);
//			centerY = event.getY(0);
//			eventMatrix.set(matrix);
//			break;
//		case MotionEvent.ACTION_POINTER_DOWN:
//			Log.i("MultitouchTestActivity", "ACTION_POINTER_DOWN, (" + event.getX(0) + ", " + event.getY(0) + "), (" + event.getX(1) + ", " + event.getY(1) + ")");
//			// secondary touch event starts: remember distance and center
//			eventDistance = calcDistance(event);
//			calcMidpoint(centerX, centerY, event);		//？？？怎么改变形参？
//			if (eventDistance > MIN_DIST) {
//				eventMatrix.set(matrix);
//				touchState = ZOOM;
//			}
//			break;
//
//		case MotionEvent.ACTION_MOVE:
//			if (touchState == DRAG) {
//				Log.i("MultitouchTestActivity", "ACTION_MOVE, (" + event.getX(0) + ", " + event.getY(0) + ")");
//				// single finger drag, translate accordingly
//				matrix.set(eventMatrix);
//				matrix.postTranslate(event.getX(0) - centerX, event.getY(0) - centerY);
//			} else if (touchState == ZOOM) {
//				Log.i("MultitouchTestActivity", "ACTION_MOVE, (" + event.getX(0) + ", " + event.getY(0) + "), (" + event.getX(1) + ", " + event.getY(1) + ")");
//				// multi-finger zoom, scale accordingly around center
//				float dist = calcDistance(event);
//				if (dist > MIN_DIST) {
//					matrix.set(eventMatrix);
//					float scale = dist / eventDistance;
//					matrix.postScale(scale, scale, centerX, centerY);
//				}
//			}
//			// Perform the transformation
//			view.setImageMatrix(matrix);
//			break;
//		case MotionEvent.ACTION_UP:
//		case MotionEvent.ACTION_POINTER_UP:
//			Log.i("MultitouchTestActivity", "ACTION_UP, (" + event.getX(0) + ", " + event.getY(0) + ")");
//			touchState = NONE;
//			break;
//		}
//		return true;
	}

	//手势监听事件,监听所有手势
	OnGestureListener gestureListener = new OnGestureListener() {
		
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			Log.i("PicGallery", "onSingleTapUp..." + e.getAction());
			return false;
		}
		
		@Override
		public void onShowPress(MotionEvent e) {
			Log.i("PicGallery", "onShowPress..." + e.getAction());
		}
		
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			Log.i("PicGallery", "onScroll...e1 = " + e1.getAction() + ", e2 = " + e2.getAction() + ", distanceX = " + distanceX + ", distanceY = " + distanceY);
//			switcher.getChildAt(selectPos).scrollBy((int)distanceX, (int)distanceY);
			ivPhoto.scrollBy((int)distanceX, (int)distanceY);
			return false;
		}
		
		@Override
		public void onLongPress(MotionEvent e) {
			Log.i("PicGallery", "onLongPress..." + e.getAction());
		}
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			Log.i("PicGallery", "onFling...e1 = " + e1.getAction() + ", e2 = " + e2.getAction() + ", velocityX = " + velocityX + ", velocityY = " + velocityY);
			return false;
		}
		
		@Override
		public boolean onDown(MotionEvent e) {
			Log.i("PicGallery", "onDown..." + e.getAction());
//			return false;	//返回false的话，下面的move, up等事件就接收不到了
			return true;	//所以，要返回true
		}
	};
	private GestureDetector detector;
	private ImageView ivPhoto;	
	
	/**
	 * 计算触摸的两点间的距离
	 * @param event
	 * @return
	 */
	private float calcDistance(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

	/**
	 * 计算触摸的两点的中点坐标
	 * @param centerX
	 * @param centerY
	 * @param event
	 */
	private void calcMidpoint(float centerX, float centerY, MotionEvent event) {
		centerX = (event.getX(0) + event.getX(1)) / 2;
		centerY = (event.getY(0) + event.getY(1)) / 2;
	}
	
}
